import scrapy
from lianjia.items import LianjiaItem


class HaidianSpider(scrapy.Spider):
    name = 'haidian'
    allowed_domains = ['bi.lianjia.com']
    start_urls = ['https://bj.lianjia.com/ershoufang/haidian/pg{}/'.format(i) for i in range(1,101)]

    def parse(self, response):

        list_all = response.xpath('//ul[@class="sellListContent"]/li')
        item = LianjiaItem()
        for li in list_all:
            item['title'] = 'haidian'
            item['hous_name'] = li.xpath('.//div[@class="title"]/a/text()').extract_first()
            item['hous_xiaoquming'] = li.xpath('.//div[@class="positionInfo"]/a[1]/text()').extract_first()
            item['hous_weizhi'] = li.xpath('.//div[@class="positionInfo"]/a[2]/text()').extract_first()
            item['hous_info'] = li.xpath('//div[@class="houseInfo"]/text()').extract_first()
            item['hous_price'] = li.xpath('//div[@class="totalPrice"]/span/text()').extract_first()
            item['hous_unitprice'] = li.xpath('//div[@class="unitPrice"]/span/text()').extract_first()
            item['hous_link'] = li.xpath('.//div[@class="title"]/a/@href').extract_first()

            yield item



